# -*- coding: utf-8 -*-


from django.conf import settings

import geoip2.database


city_reader = geoip2.database.Reader(settings.GEOLITE_CITY_DB)

def get_city(ip, lan='zh-CN'):
    try:
        city_obj = city_reader.city(ip)
        city_name = city_obj.city.names.get(lan)
        if not city_name:
            city_name = city_obj.subdivisions[0].names.get(lan)
        return city_name
    except:
        return None
